package ar.activity;

import java.util.ArrayList;
import java.util.List;

import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import ar.activity.R.string;
import ar.globals.Globals;
import ar.sql.RouteDatabaseHandler;
import ar.sql.ShareRoute;
import ar.sql.ShareRoutePoint;
import ar.view.adapter.LoadRoutesAdapter;

public class LoadRoutesActivity extends BaseActivity {

	private ListView lvLoadRoutes;
	private LoadRoutesAdapter loadRoutesAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.load_routes);
		super.onCreate(savedInstanceState);
		tvWhatScreen.setText(res.getString(string.shareLoadRoutesScreenName));

		loadRoutesAdapter = new LoadRoutesAdapter(this);
		lvLoadRoutes = (ListView) findViewById(R.id.lvLoadRoutes);
		lvLoadRoutes.setAdapter(loadRoutesAdapter);

		loadAndGroupRoutes();

		initEventClick();
	}

	public void loadAndGroupRoutes() {
		RouteDatabaseHandler myDbHelper = new RouteDatabaseHandler(this);	
		try {
			myDbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
		Globals.loadedRoutePoints = myDbHelper.getAll();
		myDbHelper.close();
		Globals.loadedRoutes = new ArrayList<ShareRoute>();
		if (Globals.loadedRoutePoints != null) {
			for (ShareRoutePoint rp : Globals.loadedRoutePoints) {
				boolean match = false;
				int index = -1;
				for (ShareRoute r : Globals.loadedRoutes) {
					if (r.getSrcName().equals(rp.getSrcName())
							&& r.getSrcPhoneNumber().equals(
									rp.getSrcPhoneNumber())
							&& r.getDesc().equals(rp.getDesc())) {
						match = true;
						index = Globals.loadedRoutes.indexOf(r);
						break;
					}
				}
				if (match == false) { // Chua ton tai route.
					ShareRoute newRoute = new ShareRoute();
					newRoute.setSrcName(rp.getSrcName());
					newRoute.setSrcPhoneNumber(rp.getSrcPhoneNumber());
					newRoute.setDesc(rp.getDesc());
					List<String> lats = new ArrayList<String>();
					List<String> lngs = new ArrayList<String>();
					lats.add(rp.getLat());
					lngs.add(rp.getLng());		
					newRoute.setLats(lats);
					newRoute.setLngs(lngs);
					Globals.loadedRoutes.add(newRoute);
				}else{
					// Ton tai route
					List<String> lats = Globals.loadedRoutes.get(index).getLats();
					List<String> lngs = Globals.loadedRoutes.get(index).getLngs();
					lats.add(rp.getLat());
					lngs.add(rp.getLng());
					Globals.loadedRoutes.get(index).setLats(lats);
					Globals.loadedRoutes.get(index).setLngs(lngs);
				}				
			}
		}
	}

	public void initEventClick() {
		lvLoadRoutes.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				
				ShareRoute r = Globals.loadedRoutes.get(position);
				//Dùng biến r để hiển thị lên map, có 1 list các lats, và 1 list các lngs tương ứng.
				int a = 1;
			}
		});
	}
}
